﻿using System.Collections.Generic;
using System.IO;
using DataScripter.Containers;
using DataScripter.Scripting.Processes.Output;

namespace DataScripter.Scripting.Processes
{
    class WriteSqlToFileProcess : BaseProcess
    {
        private string Filename { get; set; }

        public WriteSqlToFileProcess(string name, IContainer container)
            : base(name, container)
        {
        }

        public override void Initialize(Dictionary<string, string> parameters)
        {
            Filename = parameters["FileName"];
        }

        public override IProcessOutput Run()
        {
            IProcess process = InputProcesses[0];

            ISqlProcessOutput processOutput = (ISqlProcessOutput) process.Output;
            string sql = string.IsNullOrEmpty(processOutput.Sql) ? "-- No output" : processOutput.Sql;
            File.WriteAllText(Filename, sql);

            return new FileProcessOutput {Filename = Filename};
        }
    }
}
